<template>
  <div style="margin:0 0 50px 0">
    <div class="picAndWords">
      <!-- ECharts容器宽高最好固定 -->
      <div id="main1" style="width:750px;height:400px;" align="left"></div>
      <div id="aside" style="width:750px;height:400px;" align="right">
        <span>
          <strong>关键词1：让tooltip倒着显示</strong><br /><br />
          本图解决了tooltip的显示问题，当我们x轴放类别，y轴放数字的时候，tooltip显示总是反过来的。
          我们利用formatter可将数据倒过来显示:
          <br />
          <br />
          formatter: params => { // console.log(params) const len =
          params.length - 1 let name = params[0].name for (let i = len; i >= 0;
          i--) { name += '<br />' + params[i].marker + params[i].seriesName + ':
          ' + params[i].data } return name }
          <br />
          <br />
          换行后图标没有对齐？在图标div中添加 align="left" 可以解决
        </span>
      </div>
    </div>

    <div class="picAndWords">
      <!-- ECharts容器宽高最好固定 -->
      <div id="main2" style="width:750px;height:400px;" align="left"></div>
      <div id="aside" style="width:750px;height:400px;" align="right">
        <span>
          <strong>关键词2：dataZoom显示问题</strong>
          <br /><br />
          超过5个再显示，调用函数： resetDataZoom (chart, num = 5) { const len =
          10 if (len > num) { chart.setOption({ dataZoom: { type: 'slider',
          show: true, end: (100 * (len - num)) / len } }) } }
          <br /><br />
          <strong>关键词4：滑动条handleIcon图标问题</strong>
          <br /><br />
          icon图标也可以自定义：可以通过 'path://' 将图标设置为任意的矢量路径。
          <br /><br />
          barWidth设置柱形图宽度
        </span>
      </div>
    </div>

    <div class="picAndWords">
      <!-- ECharts容器宽高最好固定 -->
      <div id="main3" style="width:750px;height:400px;" align="left"></div>
      <div id="aside" style="width:750px;height:400px;" align="right">
        <span>
          <strong>关键词3：柱形图显示总和的问题</strong><br /><br />
          思路是放一个透明的柱子，然后利用函数把总和加起来：
          <br /><br />
          const series = option.series const fun = params => { var datavalue = 0
          for (let i = 0; i &lt; series.length - 1; i++) { datavalue +=
          series[i].data[params.dataIndex] } return datavalue }
          series[series.length - 1].label.formatter = fun
          <br /><br />
          这样还有一个遗留问题，就是点击图例切换的时候，总数没有跟着变化，监听图例变化即可：
          <br /><br />
          myChart.on('legendselectchanged', obj => { const selected =
          obj.selected const selectedArr = [] for (const i in selected) { //
          注意这里使用unshift selectedArr.unshift(selected[i]) } const series =
          option.series const fun = params => { var datavalue = 0 for (let i =
          0; i &lt; series.length - 1; i++) { if (selectedArr[i]) { datavalue +=
          series[i].data[params.dataIndex] } } return datavalue }
          series[series.length - 1].label.formatter = fun
          myChart.setOption(option) this.resetDataZoom(myChart) })
        </span>
      </div>
    </div>
  </div>
</template>

<script>
import echarts from 'echarts'

export default {
  data () {
    return {}
  },
  created () {},
  watch: {
    $route: {
      handler () {
        console.log('echarts watch')
      }
    }
  },

  mounted () {
    this.initEcharts1()
    this.initEcharts2()
    this.initEcharts3()
  },
  methods: {
    initEcharts1 () {
      const myChart = echarts.init(document.getElementById('main1'))
      const option = {
        // 可以自定义颜色列表
        color: [
          '#CC1CAA',
          '#8D67FF',
          '#00FFFF',
          '#48DE13',
          '#FFC516',
          '#DC3E14',
          '#8E16F8'
        ],
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'shadow'
          },
          // 用来自定义提示框内容
          formatter: params => {
            // console.log(params)
            const len = params.length - 1
            let name = params[0].name
            for (let i = len; i >= 0; i--) {
              name +=
                '<br/>' +
                params[i].marker +
                params[i].seriesName +
                ': ' +
                params[i].data
            }
            return name
          }
        },
        legend: {
          data: ['搜索引擎', '视频广告', '联盟广告', '邮件营销', '直接访问']
        },
        grid: {
          left: '3%',
          right: '4%',
          bottom: '3%',
          containLabel: true
        },
        xAxis: {
          type: 'category',
          data: ['周一', '周二', '周三', '周四', '周五', '周六', '周日']
        },
        yAxis: {
          type: 'value'
        },
        series: [
          {
            name: '直接访问',
            type: 'bar',
            barWidth: 40,
            stack: '总量',
            label: {
              show: true,
              position: 'insideRight'
            },
            data: [320, 302, 301, 334, 390, 330, 320]
          },
          {
            name: '邮件营销',
            type: 'bar',
            barWidth: 40,
            stack: '总量',
            label: {
              show: true,
              position: 'insideRight'
            },
            data: [120, 132, 101, 134, 90, 230, 210]
          },
          {
            name: '联盟广告',
            type: 'bar',
            barWidth: 40,
            stack: '总量',
            label: {
              show: true,
              position: 'insideRight'
            },
            data: [220, 182, 191, 234, 290, 330, 310]
          },
          {
            name: '视频广告',
            type: 'bar',
            barWidth: 40,
            stack: '总量',
            label: {
              show: true,
              position: 'insideRight'
            },
            data: [150, 212, 201, 154, 190, 330, 410]
          },
          {
            name: '搜索引擎',
            type: 'bar',
            barWidth: 40,
            stack: '总量',
            label: {
              show: true,
              position: 'insideRight'
            },
            data: [820, 832, 901, 934, 1290, 1330, 1320]
          }
        ]
      }
      myChart.setOption(option)
      window.addEventListener('resize', function () {
        myChart.resize()
      })
    },

    initEcharts2 () {
      const myChart = echarts.init(document.getElementById('main2'))
      const option = {
        // 可以自定义颜色列表
        color: [
          '#CC1CAA',
          '#8D67FF',
          '#00FFFF',
          '#48DE13',
          '#FFC516',
          '#DC3E14',
          '#8E16F8'
        ],
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'shadow'
          },
          // 用来自定义提示框内容
          formatter: params => {
            // console.log(params)
            const len = params.length - 1
            let name = params[0].name
            for (let i = len; i >= 0; i--) {
              name +=
                '<br/>' +
                params[i].marker +
                params[i].seriesName +
                ': ' +
                params[i].data
            }
            return name
          }
        },
        legend: {
          data: ['搜索引擎', '视频广告', '联盟广告', '邮件营销', '直接访问']
        },
        grid: {
          left: '3%',
          right: '4%',
          bottom: '8%',
          containLabel: true
        },
        xAxis: {
          type: 'category',
          data: ['周一', '周二', '周三', '周四', '周五', '周六', '周日']
        },
        yAxis: {
          type: 'value'
        },
        dataZoom: [
          {
            type: 'slider',
            start: 0,
            end: 100,
            show: false,
            showDetail: false,
            // 是否显示数据阴影
            showDataShadow: false,
            // 是否锁定选择区域（或叫做数据窗口）的大小
            zoomLock: true,
            top: 370,
            bottom: 15,
            color: '#ccc',
            backgroundColor: '#fff',
            borderColor: '#fff',
            fillerColor: '#ccc',
            handleSize: '85%',
            handleStyle: {
              borderWidth: 0,
              color: '#ccc'
            },
            handleIcon:
              'path://M512,512m-448,0a448,448,0,1,0,896,0a448,448,0,1,0,-896,0Z'
          }
        ],
        series: [
          {
            name: '直接访问',
            type: 'bar',
            barWidth: 40,
            stack: '总量',
            label: {
              show: true,
              position: 'insideRight'
            },
            data: [320, 302, 301, 334, 390, 330, 320]
          },
          {
            name: '邮件营销',
            type: 'bar',
            barWidth: 40,
            stack: '总量',
            label: {
              show: true,
              position: 'insideRight'
            },
            data: [120, 132, 101, 134, 90, 230, 210]
          },
          {
            name: '联盟广告',
            type: 'bar',
            barWidth: 40,
            stack: '总量',
            label: {
              show: true,
              position: 'insideRight'
            },
            data: [220, 182, 191, 234, 290, 330, 310]
          },
          {
            name: '视频广告',
            type: 'bar',
            barWidth: 40,
            stack: '总量',
            label: {
              show: true,
              position: 'insideRight'
            },
            data: [150, 212, 201, 154, 190, 330, 410]
          },
          {
            name: '搜索引擎',
            type: 'bar',
            barWidth: 40,
            stack: '总量',
            label: {
              show: true,
              position: 'insideRight'
            },
            data: [820, 832, 901, 934, 1290, 1330, 1320]
          }
        ]
      }
      myChart.setOption(option)
      window.addEventListener('resize', function () {
        myChart.resize()
      })
      this.resetDataZoom(myChart)
    },

    initEcharts3 () {
      const myChart = echarts.init(document.getElementById('main3'))
      const option = {
        // 可以自定义颜色列表
        color: [
          '#CC1CAA',
          '#8D67FF',
          '#00FFFF',
          '#48DE13',
          '#FFC516',
          '#DC3E14',
          '#8E16F8'
        ],
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'shadow'
          },
          // 用来自定义提示框内容
          formatter: params => {
            // console.log(params)
            const len = params.length - 1
            let name = params[0].name
            for (let i = len - 1; i >= 0; i--) {
              name +=
                '<br/>' +
                params[i].marker +
                params[i].seriesName +
                ': ' +
                params[i].data
            }
            return name
          }
        },
        legend: {
          data: ['搜索引擎', '视频广告', '联盟广告', '邮件营销', '直接访问']
        },
        grid: {
          left: '3%',
          right: '4%',
          bottom: '3%',
          containLabel: true
        },
        xAxis: {
          type: 'category',
          data: ['周一', '周二', '周三', '周四', '周五', '周六', '周日']
        },
        yAxis: {
          type: 'value'
        },
        dataZoom: [
          {
            type: 'slider',
            start: 0,
            end: 100,
            show: false,
            showDetail: false,
            // 是否显示数据阴影
            showDataShadow: false,
            // 是否锁定选择区域（或叫做数据窗口）的大小
            zoomLock: true
          }
        ],
        series: [
          {
            name: '直接访问',
            type: 'bar',
            barWidth: 40,
            stack: '总量',
            label: {
              show: false,
              position: 'insideRight'
            },
            data: [320, 302, 301, 334, 390, 330, 320]
          },
          {
            name: '邮件营销',
            type: 'bar',
            barWidth: 40,
            stack: '总量',
            label: {
              show: false,
              position: 'insideRight'
            },
            data: [120, 132, 101, 134, 90, 230, 210]
          },
          {
            name: '联盟广告',
            type: 'bar',
            barWidth: 40,
            stack: '总量',
            label: {
              show: false,
              position: 'insideRight'
            },
            data: [220, 182, 191, 234, 290, 330, 310]
          },
          {
            name: '视频广告',
            type: 'bar',
            barWidth: 40,
            stack: '总量',
            label: {
              show: false,
              position: 'insideRight'
            },
            data: [150, 212, 201, 154, 190, 330, 410]
          },
          {
            name: '搜索引擎',
            type: 'bar',
            barWidth: 40,
            stack: '总量',
            label: {
              show: false,
              position: 'insideRight'
            },
            data: [820, 832, 901, 934, 1290, 1330, 1320]
          },
          {
            name: '总数',
            type: 'bar',
            barWidth: 40,
            stack: '总量',
            itemStyle: {
              color: 'transparent'
            },
            label: {
              show: true,
              color: '#fb7299',
              position: 'insideBottom',
              formatter: params => {}
            },
            data: [120, 132, 101, 134, 90, 230, 210]
          }
        ]
      }

      // 计算总数-start
      const series = option.series
      const fun = params => {
        var datavalue = 0
        for (let i = 0; i < series.length - 1; i++) {
          datavalue += series[i].data[params.dataIndex]
        }
        return datavalue
      }
      series[series.length - 1].label.formatter = fun
      // 计算总数-end

      myChart.setOption(option)
      window.addEventListener('resize', () => {
        myChart.resize()
      })
      this.resetDataZoom(myChart)

      myChart.on('legendselectchanged', obj => {
        const selected = obj.selected
        const selectedArr = []
        for (const i in selected) {
          // 注意这里使用unshift
          selectedArr.unshift(selected[i])
        }
        const series = option.series
        const fun = params => {
          var datavalue = 0
          for (let i = 0; i < series.length - 1; i++) {
            if (selectedArr[i]) {
              datavalue += series[i].data[params.dataIndex]
            }
          }
          return datavalue
        }
        series[series.length - 1].label.formatter = fun
        myChart.setOption(option)
        this.resetDataZoom(myChart)
      })
    },
    resetDataZoom (chart, num = 5) {
      const len = 10
      if (len > num) {
        chart.setOption({
          dataZoom: {
            type: 'slider',
            show: true,
            end: (100 * (len - num)) / len
          }
        })
      }
    }
  }
}
</script>

<style lang="less" scoped>
.picAndWords {
  display: flex;
  margin-top: 20px;
  padding: 0 20px 0 10px;
  border: 1px solid #ccc;
  #aside {
    display: flex;
    margin: 0 40px;
    text-align: left;
    overflow: auto;
    span {
      margin: auto;
      padding: 10px 20px;
    }
  }
}
</style>
